package com.moli.iov.sys.remote.impl;

import com.moli.iov.exception.BusinessException;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.sys.constants.Constant;
import com.moli.iov.sys.enums.AppStatusEnum;
import com.moli.iov.sys.enums.ClientStatusEnum;
import com.moli.iov.sys.enums.CompanyStatusEnum;
import com.moli.iov.sys.enums.TrustedEnum;
import com.moli.iov.sys.model.dto.req.ClientDetailsReqDto;
import com.moli.iov.sys.model.dto.res.ClientDetailsResDto;
import com.moli.iov.sys.model.po.ClientDetails;
import com.moli.iov.sys.model.po.Company;
import com.moli.iov.sys.remote.IClientDetailsRemoteService;
import com.moli.iov.sys.service.IClientDetailsService;
import com.moli.iov.sys.service.ICompanyService;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.ValidationUtils;
import com.moli.iov.util.api.ApiUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 *  企业客户端信息 服务类
 * </p>
 *
 * @author zql
 * @since 2019/9/5 12:03
 */
@Slf4j
@Service(group = "${service.group}")
public class ClientDetailsRemoteServiceImpl implements IClientDetailsRemoteService {

  @Autowired
  private IClientDetailsService clientDetailsService;

  @Autowired
  private ICompanyService companyService;

  /**
   * 添加企业客户端信息
   * @param companyId
   * @return
   */
  @Override
  public RestResponse saveClientDetails(Long companyId) {
    ClientDetails clientDetails = new ClientDetails();
    clientDetails.setCompanyId(companyId);
    clientDetails.setSecret(ApiUtils.generateNonceStrLower(Constant.APP_SECRET_LENGTH));
    clientDetails.setStatus(ClientStatusEnum.ENABLED.getFlag());
    clientDetails.setTrusted(TrustedEnum.ON.getFlag());
    if(clientDetailsService.saveClientDetails(clientDetails) > 0){
      return RestResponse.success();
    }
    return RestResponse.failure("添加企业客户端信息失败");
  }

  /**
   * 根据appId、secret获取客户端信息
   * @param clientDetailsReqDto
   * @return
   */
  @Override
  public RestResponse<ClientDetailsResDto> getClientDetails(
      ClientDetailsReqDto clientDetailsReqDto) {

    ValidationUtils.restResponseValidate(clientDetailsReqDto, ClientDetailsReqDto.GetClientDetails.class);

    ClientDetails clientDetails = clientDetailsService.getClientDetails(clientDetailsReqDto);

    if(clientDetails == null){
      throw new BusinessException("appId不存在");
    }

    if(!clientDetails.getSecret().equals(clientDetailsReqDto.getSecret())){
      throw new BusinessException("secret错误");
    }

    if(AppStatusEnum.ENABLED.getFlag() != clientDetails.getStatus()){
      throw new BusinessException("appId已停用");
    }

    Company company = companyService.getById(clientDetails.getCompanyId());

    if(company == null){
      throw new BusinessException("企业信息异常");
    }

    if(CompanyStatusEnum.ENABLED.getFlag() != company.getStatus()){
      throw new BusinessException("企业已停用");
    }


    ClientDetailsResDto clientDetailsResDto = new ClientDetailsResDto();
    CommonUtils.copyProperties(clientDetails, clientDetailsResDto);
    clientDetailsResDto.setCompanyName(company.getCompanyName());

    return RestResponse.success(clientDetailsResDto);
  }

}
